package com.project.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.server.model.Staff;
import com.project.server.mapper.StaffMapper;
import com.project.server.service.StaffService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
@RequiredArgsConstructor
public class StaffServiceImpl implements StaffService {

    private final StaffMapper staffMapper;

    @Override
    public Page<Staff> page(Integer pageNum, Integer pageSize, String name, String phone, Long restaurantId, Integer status) {
        LambdaQueryWrapper<Staff> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.hasText(name)) {
            wrapper.like(Staff::getName, name);
        }
        if (StringUtils.hasText(phone)) {
            wrapper.like(Staff::getPhone, phone);
        }
        if (restaurantId != null) {
            wrapper.eq(Staff::getRestaurantId, restaurantId);
        }
        if (status != null) {
            wrapper.eq(Staff::getStatus, status);
        }

        wrapper.orderByDesc(Staff::getCreateTime);
        return staffMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public Staff getById(Long id) {
        return staffMapper.selectById(id);
    }

    @Override
    public void saveOrUpdate(Staff staff) {
        if (staff.getId() == null) {
            staff.setStatus(1); // 默认在职
            staffMapper.insert(staff);
        } else {
            staffMapper.updateById(staff);
        }
    }

    @Override
    public void removeById(Long id) {
        staffMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Staff staff = new Staff();
        staff.setId(id);
        staff.setStatus(status);
        staffMapper.updateById(staff);
    }








}
